﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Boiling.BizFacade;
using Boiling.Common;

namespace Boiling.Web
{
    public partial class home : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //创建流量数据，只有第一次加载首页才增加流量。
            if (Session.Contents["VisitCount"] == null)
            {
                new UserSystem().IncVisitorcount();
                Session.Add("VisitCount", new UserSystem().GetVisitorcount());
            }
            else
            {
                Session.Contents["VisitCount"] = new UserSystem().GetVisitorcount();
            }


            //登录状态回送，判断并进行不同的跳转
            if (this.IsPostBack)
            {
                string m_UserName;
                string m_PassWord;
                int checkState;
                UserSystem m_user = new UserSystem();


                m_UserName = Request.Form["name"];
                m_PassWord = Request.Form["password"];


               

                // CheckState
                //判断邮箱以及密码是否为空，均不为空才能继续
                //登录用户名不正确，返回-1
                //密码不正确，返回0
                //登录成功，返回1
                //不存在此用户，返回2

                checkState = m_user.GetUserByEmail(m_UserName, m_PassWord);

                Response.Write("<a href=" + "default.aspx title=" + "Home" + ">回到首页</a></br>");
                if (checkState == 1)
                {
                    Session.Add("userID", m_UserName);
                    //userState
                    //0：默认登录跳转
                    //1：修改完密码后跳转
                    //2：修改完用户信息跳转
                    //
                    Session.Add("userState", 0);
                    Response.Redirect("HomePage.aspx");
                }
                else
                {
                    if (Session.Contents["ErrorState"] == null)
                    {
                        Session.Add("ErrorState", checkState);
                    }
                    else
                    {
                        Session.Contents["ErrorState"] = checkState;
                    }
                    if (Session.Contents["ErrorInfo"] == null)
                    {
                        Session.Add("ErrorInfo", "未知错误!");
                    }
                    if ((int)Session.Contents["ErrorState"] == -1)
                    {
                        Session.Contents["ErrorInfo"] = "登录用户名不正确!";
                    }
                    if ((int)Session.Contents["ErrorState"] == 0)
                    {
                        Session.Contents["ErrorInfo"] = "密码不正确!";
                    }
                    if ((int)Session.Contents["ErrorState"] == 2)
                    {
                        Session.Contents["ErrorInfo"] = "不存在此用户!";
                    }
                    Response.Redirect("ErrorPage.aspx");
                }
            }
        }
        protected void ClickLogin(object sender, EventArgs e)
        {
            Response.Write("<div>Hello</div>");
            Response.Redirect("http://www.sina.com.cn");
        }
    }

    public partial class register : UserControl
    {
        protected void ClickLogin(object sender, EventArgs e)
        {
            Response.Write("<div>Hello</div>");
            Response.Redirect("http://www.sina.com.cn");
        }
    }
}
